$(function () {
    var carUrl = "/oa/t-car";
    var customerUrl = "/oa/t-customer";
    var carborrowUrl = "/oa/t-carborrow";
    var token = sessionStorage.getItem('token');
    var oprateFormatter = function (value, row, index) {
        return [
            '<button id="tb_edit" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">编辑</button>',
            '<button id="tb_delete" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">删除</button>',
        ].join("");
    };

    var oprateEvents = function(context) {
        var me = context;
        return {
            "click #tb_edit" : function(e, value, row, index) {
                me.edit([row]);
            },

            "click #tb_delete" : function(e, value, row, index) {
                me.remove([row]);
            },
        };
    };

    var stateFormatter = function (value, row, index) {
        switch (row.bizorderState) {
            case 0:
                return '开始';
            case 1:
                return '出借';
            case 2:
                return '归还';
            case 3:
                return '结束';
        }
    };

    var carList = [];
    var carFormatter = function (value, row, index) {
        var dataList = carList;
        for (var i = 0; i < dataList.length; i++) {
            var data = dataList[i];
            if (row.carId == data.carId) {
                return data.carName;
            }
        }

        return row.roleId;
    };

    var carLoader = function (me, field) {
        $(field).empty();
        var elem = $(field);
        var dataList = carList;
        elem.append('<option value="">请选择...</option>');
        for (var i = 0; i < dataList.length; i++) {
            var data = dataList[i];
            elem.append(
                '<option value="'
                + data.carId
                + '">'
                + data.carId
                + '</option>'
            );
        }
    };

    var customerList = [];
    var customerFormatter = function (value, row, index) {
        var dataList = customerList;
        for (var i = 0; i < dataList.length; i++) {
            var data = dataList[i];
            if (row.customerId == data.id) {
                return data.cname;
            }
        }

        return row.customerId;
    };

    var custormerLoader = function (me, field) {
        $(field).empty();
        var elem = $(field);
        var dataList = customerList;
        elem.append('<option value="">请选择...</option>');
        for (var i = 0; i < dataList.length; i++) {
            var data = dataList[i];
            elem.append(
                '<option value="'
                + data.id
                + '">'
                + data.cname
                + '</option>'
            );
        }
    };

    var page = $.fn.cookePageTable({
        URL: carborrowUrl,
        dialog: {
            domid: '#addModel',
            title: '车辆借用',
            validate : {
                carName: {
                    required: true,
                },
                customerId: {
                    required: true,
                    minlength: 1,
                    maxlength: 128,
                },
                bizorderState: {
                    required: true,
                    digits:true,
                },
            },

            fieldLoader: {
                carId : carLoader,
                customerId: custormerLoader,
            },
        },

        toolbar: {
            add: '#add',
            del: '#del',
            edit: '#edit',
            domid: '#toolbar',
            search: '#search',
        },

        table: {
            domid: "#table",
            uniqueId: "id", //每一行的唯一标识，一般为主键列
            uniqdesc: function(row) {
                return row.id;
            },                  //每一行的唯一标识描述字段信息
            columns: [{
                checkbox: true
            }, {
                field: 'carId',
                title: '车牌号码',
                filter: true,
            }, {
                field: 'carId',
                title: '车辆名称',
                filter: true,
                formatter: carFormatter,
            }, {
                field: 'customerId',
                title: '客户姓名',
                filter: true,
                formatter: customerFormatter,
            },  {
                field: 'createTime',
                title: '创建时间',
            }, {
                field: 'borrowTime',
                title: '出借时间',
                filter: true,
            }, {
                field: 'carbackTime',
                title: '还车时间',
                filter: true,
            },  {
                field: 'endTime',
                title: '结束时间',
            }, {
                field: 'bizorderState',
                title: '订单状态',
                formatter: stateFormatter,
            }, {
                field: 'operate',
                title: '操作',
                visible: true,
                events: oprateEvents,
                formatter: oprateFormatter,
            }]
        },
    });

    page.listCarList = function (successFun) {
        var me = this;
        $.ajax({
            url: carUrl + '/listAll',
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify({
            }),
            headers: {Token: token},
            success: function (data) {
                if (data.code != 200){
                    $.message({
                        message : data.msg || data.error,
                        type:'error'
                    });
                    return;
                }

                if (successFun) {
                    successFun(me, data.data);
                }
            },
        });
    }; // end of page.resetPwd

    page.listCustomerList = function (successFun) {
        var me = this;
        $.ajax({
            url: customerUrl + '/listAll',
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify({
            }),
            headers: {Token: token},
            success: function (data) {
                if (data.code != 200){
                    $.message({
                        message : data.msg || data.error,
                        type:'error'
                    });
                    return;
                }

                if (successFun) {
                    successFun(me, data.data);
                }
            },
        });
    }; // end of page.listCustomerList

    page.listCarList(function (me, data) {
        carList = data;
        me.refresh();
    });

    page.listCustomerList(function (me, data) {
        customerList = data;
        me.refresh();
    });
});
